package cn.seapon.talker;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import cn.seapon.talker.net.HttpRequest;

public class RegisterAccountActivity extends Activity implements
		OnClickListener {

	private static final String TAG = "||__RegisterAccountActivity__||";
	private static final String CHECK_URL = "http://1.lichtung.sinaapp.com/appserver/appservice.php?request_code=2001&";

	private EditText et_name;
	private EditText et_pwd;
	private EditText et_pwd_again;
	private Button next_btn;
	private Button check_btn;
	
	private boolean clickable = true;
	private RelativeLayout wait_net_frame = null;
	
	private static final int NAME_USABLE = 1;
	private static final int NAME_UNUSABLE = -1;
	private static final int PASSWD_CANTBE_NULL = -2;
	private static final int PASSWD_DIFFERENT = -3;
	private static final int GO_NEXR = 3;
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			clickable = true;
			wait_net_frame.setVisibility(View.GONE);
			switch (msg.what) {
			case NAME_UNUSABLE:
				Toast.makeText(RegisterAccountActivity.this, "该用户名已经被注册了！", Toast.LENGTH_SHORT).show();
				break;
			case NAME_USABLE:
				Toast.makeText(RegisterAccountActivity.this, "该用户名可以使用！", Toast.LENGTH_SHORT).show();
				break;
				
			case 2:
				
				break;
			case PASSWD_CANTBE_NULL:
				et_pwd.setText("");
				et_pwd_again.setText("");
				Toast.makeText(RegisterAccountActivity.this, "密码不能为空!", Toast.LENGTH_SHORT).show();
				break;
			case PASSWD_DIFFERENT :
				et_pwd.setText("");
				et_pwd_again.setText("");
				Toast.makeText(RegisterAccountActivity.this, "密码不能为空，且前后出入需要一致!", Toast.LENGTH_SHORT).show();
				break;
			case GO_NEXR:

				Intent intent = new Intent();
				intent.setClass(RegisterAccountActivity.this, RegisterPersonalActivity.class);
				intent.putExtra("account_name", et_name.getText().toString().trim());
				intent.putExtra("pwd", et_pwd.getText().toString().trim());
				startActivity(intent);
				break;
			default:
				break;
			}
		}
		
	};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_account);
		et_name = (EditText) findViewById(R.id.account_name);
		et_pwd = (EditText) findViewById(R.id.account_pwd);
		et_pwd_again = (EditText) findViewById(R.id.account_pwd_again);
		next_btn = (Button) findViewById(R.id.next_btn);
		check_btn = (Button) findViewById(R.id.check_btn);
		wait_net_frame = (RelativeLayout) findViewById(R.id.wait_net_layout);

		next_btn.setOnClickListener(this);
		check_btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (!clickable) {//防止多次点击
			Toast.makeText(this, "正在请求，请稍后！", Toast.LENGTH_SHORT).show();
			return ;
		}
		wait_net_frame.setVisibility(View.VISIBLE);
		switch (v.getId()) {
		case R.id.next_btn:
			clickable = false;
			final Message message = Message.obtain();
			final String name = et_name.getText().toString().trim();
			final String pwd = et_pwd.getText().toString();
			final String pwdagain = et_pwd_again.getText().toString();
			//检查是否为空
			if (TextUtils.isEmpty(name)) {
				Toast.makeText(this, "账号名不能为空！", Toast.LENGTH_SHORT).show();
				return;
			}
			Pattern pattern = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9_]{5,15}$");
			Matcher matcher = pattern.matcher(name);
			//检查是否符合规范
			if (!matcher.matches()) {
				Toast.makeText(this, "该用户名不符合规范！", Toast.LENGTH_SHORT).show();
				return;
			}

			if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwdagain) ) {
				message.what = PASSWD_CANTBE_NULL;
				handler.sendMessage(message);
				return;
			} 
			if (!pwd.equals(pwdagain) ){
				message.what = PASSWD_DIFFERENT;
				handler.sendMessage(message);
				return;
			}
			final String url = CHECK_URL+"userid="+name;
			new Thread(){

				@Override
				public void run() {
					String retString = HttpRequest.doGetRequest(url);
					try {
						Log.d(TAG, "返回的字符串是："+retString);
						if (Integer.valueOf(retString) > 0) {
							message.what = NAME_UNUSABLE;
						}else {
							message.what = GO_NEXR;
						}
					} catch ( NumberFormatException e) {
						e.printStackTrace();
						message.what = NAME_UNUSABLE;
					}
					handler.sendMessage(message);
				}
				
			}.start();
			break;
		case R.id.check_btn:
			checkNameUsable();
			break;

		default:
			break;
		}
	}

	private void checkNameUsable() {
		Log.d(TAG, "checkNameUsable");
		final Message message = Message.obtain();
		String name = et_name.getText().toString().trim();
		//检查是否为空
		if (TextUtils.isEmpty(name)) {
			Toast.makeText(this, "账号名不能为空！", Toast.LENGTH_SHORT).show();
			return;
		}
		Pattern pattern = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9_]{5,15}$");
		Matcher matcher = pattern.matcher(name);
		//检查是否符合规范
		if (!matcher.matches()) {
			Toast.makeText(this, "该用户名不符合规范！", Toast.LENGTH_SHORT).show();
			return;
		}
		final String url = CHECK_URL+"userid="+name;
		Log.d(TAG, "请求的网址是："+url);
		new Thread(){

			@Override
			public void run() {
				String retString = HttpRequest.doGetRequest(url);
				try {
					//返回的是数量
					Log.d(TAG, "返回的字符串是："+retString);
					if (Integer.valueOf(retString) > 0) {
						message.what = NAME_UNUSABLE;
					}else {
						message.what = NAME_USABLE;
					}
				} catch ( NumberFormatException e) {
					e.printStackTrace();
					message.what = NAME_UNUSABLE;
				}
				handler.sendMessage(message);
			}
		}.start();

	}

}
